// ChassisSendData.js

class ChassisSendData {
    constructor() {
        this.webSocketHandler = window.webSocketHandler;
        if (!this.webSocketHandler) {
            throw new Error('WebSocketHandler not initialized');
        }
    }

    /**
     * 发送机器人控制命令
     * @param {string} action - 动作类型
     * @param {Object} params - 参数对象
     */
    sendControlCommand(action, params = {}) {
        try {
            switch (action) {
                case 'start':
                    this.sendChassisControl('START');
                    break;
                case 'stop':
                    this.sendChassisControl('STOP');
                    break;
                case 'pause':
                    this.sendChassisControl('PAUSE');
                    break;
                case 'move':
                    this.sendMotionCommand(params);
                    break;
                case 'setMode':
                    this.sendSetModeCommand(params.mode);
                    break;
                case 'setWaterLevel':
                    this.sendWaterControl(params);
                    break;
                case 'setSuctionPower':
                    this.sendSuctionControl(params);
                    break;
                case 'dock':
                    this.sendChassisControl('DOCK');
                    break;
                case 'findRobot':
                    this.sendChassisControl('FIND');
                    break;
                case 'deleteMap':
                    this.sendMapCommand('DELETE');
                    break;
                default:
                    console.warn('Unknown command action:', action);
            }
        } catch (error) {
            console.error('Error sending command:', error);
            throw error;
        }
    }

    /**
     * 发送底盘控制命令
     * @param {string} command - 控制命令
     * @param {Object} extraParams - 额外参数
     */
    sendChassisControl(command, extraParams = {}) {
        this.webSocketHandler.sendMessage('ChassisControlCommand', {
            command,
            timestamp: Date.now(),
            ...extraParams
        });
    }

    /**
     * 发送运动控制命令
     * @param {Object} params - 运动参数
     */
    sendMotionCommand(params) {
        this.webSocketHandler.sendMessage('ChassisMotionCommand', {
            linearX: params.linearX || 0,
            linearY: params.linearY || 0,
            angular: params.angular || 0,
            timeStamp: Date.now()
        });
    }

    /**
     * 发送模式设置命令
     * @param {string} mode - 工作模式
     */
    sendSetModeCommand(mode) {
        this.webSocketHandler.sendMessage('ChassisSetModeCommand', {
            mode,
            timestamp: Date.now()
        });
    }

    /**
     * 发送水量控制命令
     * @param {Object} params - 水量参数
     */
    sendWaterControl(params) {
        this.webSocketHandler.sendMessage('WaterControlCommand', {
            level: params.level,
            enabled: true,
            timestamp: Date.now()
        });
    }

    /**
     * 发送吸力控制命令
     * @param {Object} params - 吸力参数
     */
    sendSuctionControl(params) {
        this.webSocketHandler.sendMessage('SuctionControlCommand', {
            power: params.power,
            enabled: true,
            timestamp: Date.now()
        });
    }

    /**
     * 发送地图操作命令
     * @param {string} operation - 地图操作类型
     */
    sendMapCommand(operation) {
        this.webSocketHandler.sendMessage('MapCommand', {
            operation,
            timestamp: Date.now()
        });
    }

    /**
     * 发送组合命令
     * @param {Array} commands - 命令数组
     */
    sendCompositeCommand(commands) {
        commands.forEach(cmd => {
            this.sendControlCommand(cmd.action, cmd.params);
        });
    }
}

// 创建全局单例
const chassisSendData = new ChassisSendData();

// 导出到全局作用域
if (typeof window !== 'undefined') {
    window.chassisSendData = chassisSendData;
}

// CommonJS 模块导出
if (typeof module !== 'undefined' && module.exports) {
    module.exports = {
        ChassisSendData,
        chassisSendData
    };
}